package com.victor.controller.common;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.victor.entity.collection.Collections;
import com.victor.service.CollectionService;
import com.victor.utils.ClassFilter;
import com.victor.utils.Msg;

@RestController
@RequestMapping("/collection")
public class CollectionController {
	@Autowired
	private CollectionService service;
	/**
	 * 添加取消收藏
	 * @param collections
	 * @param flag 
	 * @return
	 */
	@RequestMapping("/addcollection")
	public Msg addCoccection(Collections collections,String flag) {
		String[] str = {"uid","fid","pid"};
		boolean empty = ClassFilter.somePropertyIsEmpty(collections, str);
		if (empty == true || flag == null || flag.isEmpty()) {
			return Msg.fail("入参错误");
		}
		//城市
		if (flag=="0" || flag.equals("0")) {
			collections.setPid("c_id="+collections.getPid());
			collections.setTable_name("c_cityintroduce");
			
		}
		//景区
		if (flag=="1" || flag.equals("1")) {
			collections.setPid("id="+collections.getPid());
			collections.setTable_name("jingqulist");
			
		}
		//宾馆  美食  娱乐
		if (flag=="2" || flag.equals("2")) {
			collections.setPid("p_id="+collections.getPid());
			collections.setTable_name("partner_user");
			
		}
		//待定
//		if (flag=="0" || flag.equals("0")) {
//			collections.setPid("r_id="+collections.getPid());
//			collections.setTable_name("c_cityintroduce");
//			
//		}
//		if (flag=="0" || flag.equals("0")) {
//			collections.setPid("r_id="+collections.getPid());
//			collections.setTable_name("c_cityintroduce");
//			
//		}
		//查询我是否收藏
		Collections selCollections = service.selCollections(collections);
		if (selCollections == null) {
			boolean insertCollection = service.insertCollection(collections);
			if (!insertCollection) {
				return Msg.fail("添加收藏失败");
			}
			return Msg.success().add("return", "收藏成功");
		}else {
			boolean delCollection = service.delCollection(selCollections.getId());
			if (!delCollection) {
				return Msg.fail("删除收藏失败");
			}
			return Msg.success().add("return", "取消收藏成功");
		}
		
		
	}

}
